<template>
  <div class="login">
    <div class="center">
      <h2>登陆界面</h2>
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm" :status-icon="true" size="medium">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-service"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" >
          <el-input v-model="form.password" prefix-icon="el-icon-service" suffix-icon="el-icon-view"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "login",
    data() {
      return {
        form: {
          username: '',
          password: ''
        },
        rules: {
          // username:[ { required: true, message: '请输入账号', trigger: 'blur' },
          //   { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }],
          // password:[ { required: true, message: '请输入密码', trigger: 'blur' },
          //   { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {//验证通过提交数据
            this.axios({
              method: 'post',
              url: '/api/userlogin',
              data: this.form
            }).then(res => {
              if(res.code != 200) {//数据提交失败
                this.$message({
                  type: 'info',
                  message: res.msg
                });
              }else{//登陆成功后
                console.log(res.list, 'userInfo---------')
                this.$store.commit('userinfo',res.list)
                this.$router.push('/')
              }
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped >
  .login{
    width: 100%;
    height: 100%;
    background-color: #000;
    overflow: hidden;
  }
  h2{
    margin:20px ;
    font-size: 40px ;
    text-align: center;

  }
  .center{
    background-color: white;
    width: 500px;
    height: 300px ;
    box-sizing: border-box;
    border-radius: 20px;
    margin: 60px auto;
    box-shadow: 6px 8px 10px 5px rgba(255,255,255,.5);
    overflow: hidden;
  }
  .el-form{
    margin-right: 20px ;
  }
  .el-button{
    width: 150px;
    height: 50px;
  }
</style>
